package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.mapper.*;
import com.example.teachingSystem.pojo.entity.*;
import com.example.teachingSystem.pojo.form.BasePageQuery;
import com.example.teachingSystem.pojo.form.notice.AddNoticeForm;
import com.example.teachingSystem.pojo.form.notice.UpdateNoticeForm;
import com.example.teachingSystem.service.NoticeService;
import com.example.teachingSystem.utils.JwtHelper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import com.example.teachingSystem.utils.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 86147
* @description 针对表【notice(系统通知表)】的数据库操作Service实现
* @createDate 2023-11-27 21:43:54
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public Result<String> addNotice(AddNoticeForm addNoticeForm){
        Notice notice = new Notice();
        BeanUtils.copyProperties(addNoticeForm,notice);
        if(notice.getIsActive()){
            notice.setPublishTime(new Date());
        }
        this.save(notice);
        return Result.ok(null,"添加成功");
    }

    @Override
    public Result<String> updateNotice(UpdateNoticeForm updateNoticeForm){
        Notice one = this.getOne(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getNoticeId, updateNoticeForm.getNoticeId()));
        if (one == null) {
            return Result.build(null, 500, "不存在该学期");
        }
        BeanUtils.copyProperties(updateNoticeForm, one);
        this.updateById(one);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> deleteNotice(Long id){
        Notice one = this.getOne(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getNoticeId, id));
        if (one == null) {
            return Result.build(null, 500, "不存在该学期");
        }
        this.removeById(one);
        return Result.ok(null, "删除成功");
    }

    @Override
    public Result<String> changeNoticeActive(Long noticeId,Boolean isActive){
        this.update(new LambdaUpdateWrapper<Notice>()
                .eq(Notice::getNoticeId,noticeId).set(Notice::getIsActive,isActive)
                .set(Notice::getPublishTime,new Date()));
        return Result.ok(null, "修改成功");
    }

    @Override
    public PageResult<Notice> getNoticePage(BasePageQuery basePageQuery){
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getAdminId, Long.parseLong(user.getUsername())));
        if(admin != null){
            return getNoticePage(basePageQuery,false,false);
        }
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getTeacherId,Long.parseLong(user.getUsername())));
        if(teacher != null){
            return getNoticePage(basePageQuery,false,true);
        }
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStudentId,Long.parseLong(user.getUsername())));
        if(student!= null){
            return getNoticePage(basePageQuery,true,false);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

    public PageResult<Notice> getNoticePage(BasePageQuery basePageQuery, boolean isStu, Boolean isTea){
        Page<Notice>  page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        if(isStu){
            IPage<Notice> noticeIPage = this.page(page,new LambdaQueryWrapper<Notice>()
                    .eq(Notice::getIsActive,true)
                    .eq(Notice::getType,1).or().eq(Notice::getType,3));
            return PageResult.success(noticeIPage);
        }else if(isTea){
            IPage<Notice> noticeIPage = this.page(page,new LambdaQueryWrapper<Notice>()
                    .eq(Notice::getIsActive,true)
                    .eq(Notice::getType,1).or().eq(Notice::getType,2));
            return PageResult.success(noticeIPage);
        }
        IPage<Notice> noticeIPage = this.page(page);
        return PageResult.success(noticeIPage);
    }

}




